// pages/nav/west_log/west_log.js
const mybehavior = require("../../../behaviors/requests")
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    page: 1,
    value2: "",
    total: 0,
    page_all: 0,
    base_url: "https://logisticsengineering.gree.com/andon_system",
    location: "一车间汇总",
    materialPickerValue: [],
    list: [],
    option1: [],
    value1: '一车间汇总',
    show: false,
    detail_list: [],
    steps: [{
      text: '创建中',
      desc: '',
    },
    {
      text: '正在上料',
      desc: '',

    },
    {
      text: '前往取料',

    },
    {
      text: '正在拉料',
      desc: '',

    },
    {
      text: '确认收料',
      desc: '',

    },
    {
      text: '已完成',
      desc: '',

    },
    ],
    linePickerValue: [],
    title: "一车间汇总",
    title1: "",
    title2: "",
    title3: "",
    title4: "上一次",
    active: 0

  },
  pageUp() {
    let now_page = this.data.page
    let val = this.data.title
    if (now_page != 1) {
      now_page = now_page - 1
      this.setData({
        page: now_page,
      })
      this.getDatas(val)
    }


  },
  pageDown() {
    let now_page = this.data.page
    let val = this.data.title
    if (now_page < this.data.page_all) {
      now_page = now_page + 1
      this.setData({
        page: now_page
      })
      this.getDatas(val)
    }

  },
  // 切换上次、本次详情
  detailSelect() {
    let t = this.data.title4
    let data_arr = this.data.detail_list
    let step_arr = this.data.steps
    if (t == "上一次") {
      this.setData({
        title4: "回到本次",
        title3: data_arr[1].all_times
      })
      step_arr[0].desc = data_arr[1].start_time
      step_arr[1].desc = data_arr[1].state_time1
      step_arr[2].desc = data_arr[1].state_time2
      step_arr[3].desc = data_arr[1].state_time3
      step_arr[4].desc = data_arr[1].state_time4
      step_arr[5].desc = data_arr[1].over_time
      this.setData({
        steps: step_arr,
        active: 5
      })
    } else {
      this.setData({
        title3: data_arr[0].all_times,
        title4: "上一次"
      })
      step_arr[0].desc = data_arr[0].start_time
      step_arr[1].desc = data_arr[0].state_time1
      step_arr[2].desc = data_arr[0].state_time2
      step_arr[3].desc = data_arr[0].state_time3
      step_arr[4].desc = data_arr[0].state_time4
      step_arr[5].desc = data_arr[0].over_time
      this.setData({
        active: data_arr[0].step,
        steps: step_arr
      })
    }
  },
  // 详情弹出框
  onCaoZuo(e) {
    let row = e.currentTarget.dataset.row_data
    console.log(row);
    this.setData({
      show: true,
      title1: row.location,
      title2: row.material_name,
      title3: row.all_times,
    })
    let that = this
    let url = this.data.base_url + "/west_progress_bar"
    let data = {
      location: this.data.value1,
      material_location: row.location,
      material_name: row.material_name,
      current: 1,
    }
    this.get(url, data).then(res => {
      const result = res;
      const code = result.code;
      if (code == 20000) {
        // console.log("11", result);
        that.setData({
          detail_list: result.pop_data
        })
        console.log(that.data.detail_list);
        let step_arr = that.data.steps
        step_arr[0].desc = that.data.detail_list[0].start_time
        step_arr[1].desc = that.data.detail_list[0].state_time1
        step_arr[2].desc = that.data.detail_list[0].state_time2
        step_arr[3].desc = that.data.detail_list[0].state_time3
        step_arr[4].desc = that.data.detail_list[0].state_time4
        step_arr[5].desc = that.data.detail_list[0].over_time
        that.setData({
          steps: step_arr,
          active: that.data.detail_list[0].step
        })

      } else {
        wx.showToast({
          title: result.msg,
          icon: 'error'
        })
      }
    })
  },

  onClose() {
    this.setData({
      show: false,
      title1: "",
      title2: "",
      title3: "",
      title4: "上一次",
      active: 0
    })
  },
  onDropdownChange2(e) {
    let that = this;
    this.setData({
      list:[],
      value2:""
    })
    console.log(e.detail);
    this.get('http://logisticsengineering.gree.com/andon_system/get_log_info', {
      location: this.data.value1,
      material_name: e.detail,
      current: this.data.page,
      token: wx.getStorageSync('andon_token')
    }).then(res => {
      that.setData({
        list: res.loginfo,
        value2: e.detail,
        total: res.log_total,
        page_all: Math.ceil(res.log_total / 10)
      })
    })

  },
  // 下拉筛选框
  onDropdownChange1(e) {
    let that = this;
    this.setData({
        list:[],
        value2:"",
        materialPickerValue: []
    })
    console.log(e.detail);
    this.get('http://logisticsengineering.gree.com/andon_system/get_material_list', {
      location: e.detail
    }).then(res => {
      let l = [];
      for (let i = 0; i < res.data.length; i++) {
        l.push({
          text: res.data[i],
          value: res.data[i]
        })
      }
      that.setData({
        value1: e.detail,
        materialPickerValue: l
      })
    })
  },
  // 获取数据
  getDatas(ev) {
    let that = this
    let url = this.data.base_url + "/west_change_table"
    let data = {
      location: ev,
      current: this.data.page,
    }
    this.get(url, data).then(res => {
      const result = res;
      const code = result.code;
      if (code == 20000) {
        // console.log("11", result);
        that.setData({
          list: result.table_data,
          total: result.table_total,
          page_all: Math.ceil(result.table_total / 10)
        })

      } else {
        wx.showToast({
          title: result.msg,
          icon: 'error'
        })
      }
    })
  },
  // 获取西区线体数据
  getLineDatas() {
    let that = this
    let url = this.data.base_url + "/get_west_line_list"
    let data = {}
    this.get(url, data).then(res => {
      const result = res;
      const code = result.code;
      if (code == 20000) {
        console.log("11", result);
        let r = result.data
        let options = that.data.option1
        for (let i in r) {
          let val_arr = {
            text: '',
            value: ''
          }
          val_arr.text = r[i]
          val_arr.value = r[i]
          options.push(val_arr)
        }
        that.setData({
          option1: options
        })

      } else {
        wx.showToast({
          title: result.msg,
          icon: 'error'
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const factory = options.factory
    console.log(factory)
    let that = this;
    this.get('http://logisticsengineering.gree.com/andon_system/get_line_list', {"factory":factory}).then(res => {
      let l = [];
      for (let i = 0; i < res.data.length; i++) {
        l.push({
          text: res.data[i],
          value: res.data[i]
        })
      }
      that.setData({
        linePickerValue: l
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})